﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Text;
using System.Xml;

namespace iPhotoUploader.Library
{
    public class AlbumImage
    {
        public string Id { get; set; }
        public string Name { get; set; }
        public string Caption { get; set; }
        public string ImagePath { get; set; }
        public string OriginalPath { get; set; }
        public string ThumbPath { get; set; }
        public bool IsVideo { get; set; }
        public DateTime Date { get; set; }
        // = 1 web
        // = 2 web and facebook
        // = 3 web and facebook
        // = 4 web and facebook
        // = 5 web, facebook and favorite
        public int Rating { get; set; } 
        public string Latitude { get; set; }
        public string Longitude { get; set; }
        public List<int> FaceIds { get; set; }
        public bool IsWeb
        {
            get
            {
                if (Rating >= 1)
                    return true;

                return false;
            }
        }
        public bool IsFacebook
        {
            get
            {
                if (Rating >= 2)
                    return true;

                return false;
            }
        }
        public bool IsFavorite
        {
            get
            {
                if (Rating == 5)
                    return true;

                return false;
            }
        }
        public AlbumImage() { }

        public AlbumImage(XmlNode node)
        {
            if (node.SelectSingleNode("key[normalize-space(text())='Caption']") != null)
                Name = node.SelectSingleNode("key[normalize-space(text())='Caption']").NextSibling.InnerText;

            if (node.SelectSingleNode("key[normalize-space(text())='ImagePath']") != null)
            {
                ImagePath = Utility.CheckOSPath(node.SelectSingleNode("key[normalize-space(text())='ImagePath']").NextSibling.InnerText);
                IsVideo = Utility.CheckVideoExtension(ImagePath);
            }

            if (node.SelectSingleNode("key[normalize-space(text())='OriginalPath']") != null)
                OriginalPath = node.SelectSingleNode("key[normalize-space(text())='OriginalPath']").NextSibling.InnerText;

            if (node.SelectSingleNode("key[normalize-space(text())='ThumbPath']") != null)
            {
                ThumbPath = Utility.CheckOSPath(node.SelectSingleNode("key[normalize-space(text())='ThumbPath']").NextSibling.InnerText);
            }

            if (node.SelectSingleNode("key[normalize-space(text())='Comment']") != null)
                Caption = node.SelectSingleNode("key[normalize-space(text())='Comment']").NextSibling.InnerText;

            if (node.SelectSingleNode("key[normalize-space(text())='Rating']") != null)
                Rating = Int32.Parse(node.SelectSingleNode("key[normalize-space(text())='Rating']").NextSibling.InnerText);

            if (node.SelectSingleNode("key[normalize-space(text())='latitude']") != null)
                Latitude = node.SelectSingleNode("key[normalize-space(text())='latitude']").NextSibling.InnerText;

            if (node.SelectSingleNode("key[normalize-space(text())='longitude']") != null)
                Longitude = node.SelectSingleNode("key[normalize-space(text())='longitude']").NextSibling.InnerText;

            if (node.SelectSingleNode("key[normalize-space(text())='DateAsTimerInterval']") != null)
            {
                string dateString = node.SelectSingleNode("key[normalize-space(text())='DateAsTimerInterval']").NextSibling.InnerText;

                double interval = Convert.ToDouble(dateString);
                var epoch = new DateTime(2001, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddSeconds(interval);
                Date = new DateTime(epoch.Year, epoch.Month, epoch.Day);
            }

            FaceIds = new List<int>();
            if (node.SelectSingleNode("key[normalize-space(text())='Faces']") != null)
            {
                FaceIds = new List<int>();
                XmlNode array = node.SelectSingleNode("key[normalize-space(text())='Faces']").NextSibling;
                foreach (XmlNode dict in array.ChildNodes)
                {
                    FaceIds.Add(Int32.Parse(dict.SelectSingleNode("key[normalize-space(text())='face key']").NextSibling.InnerText));
                }
            }
        }

        public static List<AlbumImage> LoadAllImages(XmlDocument xml)
        {
            List<AlbumImage> allImages = new List<AlbumImage>();
            XmlNodeList masterImages = xml.SelectSingleNode("plist/dict/key[normalize-space(text())='Master Image List']").NextSibling.ChildNodes;

            foreach (XmlNode masterImage in masterImages)
            {
                if (masterImage.Name == "key")
                {
                    string id = masterImage.InnerText;
                    XmlNode imageNode = masterImage.NextSibling;
                    AlbumImage image = new AlbumImage(imageNode);
                    image.Id = id;
                    allImages.Add(image);
                }
            }

            return allImages;
        }
    }
}
